﻿using Autofac;
using Autofac.Integration.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Alos.Entity;
using Newtonsoft.Json;

namespace Alos.LanClass
{
    /// <summary>
    /// 应用程序全局配置
    /// </summary>
    public class WebApiApplication : System.Web.HttpApplication
    {
        /// <summary>
        /// 应用程序入口
        /// </summary>
        protected void Application_Start()
        {
            SetAutofacWebAPI();
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
        /// <summary>
        /// Autofac依赖注入
        /// </summary>
        private void SetAutofacWebAPI()
        {
            var builder = new ContainerBuilder();
            HttpConfiguration config = GlobalConfiguration.Configuration;
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyTypes(Assembly.Load("Alos.IRepository"), Assembly.Load("Alos.Repository"))
             .Where(t => t.Name.EndsWith("Repository"))
             .AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(Assembly.Load("Alos.IService"), Assembly.Load("Alos.Service"))
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces();

            var container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
        /// <summary>
        /// 请求开始
        /// </summary>
        public void Application_BeginRequest()
        {
            string abpath = Request.Url.AbsolutePath.ToLower();
            if (abpath.IndexOf("/api/") == 0 && abpath.IndexOf("api/userinfo/login") == -1)
            {
                var au = Common.Current.GetAuthUser();
                if (au == null)
                {
                    HttpContext.Current.Response.ContentType = "application/Json";
                    HttpContext.Current.Response.Write(JsonConvert.SerializeObject(new JsonRtn<string> { state = RtnState.fail, message = "用户未登陆！" }));
                    HttpContext.Current.Response.End();
                }
                else if (au.overdue)
                {
                    HttpContext.Current.Response.ContentType = "application/Json";
                    HttpContext.Current.Response.Write(JsonConvert.SerializeObject(new JsonRtn<string> { state = RtnState.fail, message = "登陆已过期！" }));
                    HttpContext.Current.Response.End();
                }
            }
            //if (!(Request.HttpMethod.ToUpper() == "OPTIONS" && Request.Headers["Access-Control-Request-Headers"] != null && Request.Headers["Access-Control-Request-Headers"].ToLower() == "sessionkey"))
            //{
                
            //}
        }
    }
}
